﻿///<reference path="jquery-1.4.3.min.js" />
///<reference path="kooboo.js" />
///<reference path="kooboo.webcontrols.js" />

function __InitCheckboxlist(ui, defautValues,id) {

    kooboo.webcontrols[ui] = {
        val: function (value) {
            if (value == undefined) {
                var values = [];
                $("#" + ui + " input[type=checkbox]").each(function (index, item) {
                    $item = $(item);
                    if ($item.attr("checked") == true) {
                        values.push($item.val());
                    }
                });

                return values.toString();
            } else {

                var values = [];

                if (value instanceof Array) {
                    for (var i = 0; i < value.length; i++) {
                        values.push(value[i]);
                    }
                } else {
                    values = value.split(",");
                }

                $("#" + ui + " input[type=checkbox]").each(function (index, item) {
                    $item = $(item);
                    var checkedValue = $item.val();
                    for (var i in values) {
                        if (values[i] == checkedValue) {
                            $item.attr("checked", true);
                            return;
                        }
                    }

                    $item.attr("checked", false);

                });
            }
        },
        html: function (html) {
            this.val(html);
        },
        reset: function () {
            var defaults = defautValues.split(",");
            this.val(defaults);
        },
        id: id
    };
}
